package com.neiquan.neiquandemo.base;

import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.RelativeLayout;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.neiquan.neiquandemo.R;

import net.neiquan.applibrary.base.BaseActivity;
import net.neiquan.applibrary.wight.RefreshLayout;

/**
 * Created by wl_user on 2017/7/11.
 */

public abstract class BaseListActivity extends BaseActivity implements RefreshLayout.RetryListener, XRecyclerView.LoadingListener {
    protected int pageNum = 1;
    protected static int PAEG_SIZE = 10;
    protected View view;
    protected MyBaseAdapter adapter;
    protected XRecyclerView mRecyclerview;
    RelativeLayout mHeadView;
    protected RefreshLayout mRefeshLy;

    @Override
    public int getRootViewId() {
        return R.layout.base_common_xlistview;
    }

    @Override
    public void setViews() {
        mRecyclerview = (XRecyclerView) findViewById(R.id.recyclerview);
        mHeadView = (RelativeLayout) findViewById(R.id.head_view);
        mRefeshLy = (RefreshLayout) findViewById(R.id.refesh_ly);
        setAdapter();
    }

    private void setAdapter() {
        setHeadView(isHaveHead());
        setHeadViews();
        adapter = getAdapter();
        mRecyclerview.setAdapter(adapter);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(getLineNum() == 0 ? 1 : getLineNum(),
                StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(StaggeredGridLayoutManager.VERTICAL);
        mRecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerview.setArrowImageView(R.mipmap.iconfont_downgrey);
        mRecyclerview.setHasFixedSize(true);
        mRecyclerview.setLayoutManager(layoutManager);
        mRefeshLy.setRetryListener(this);

        mRecyclerview.setLoadingListener(this);
        mRecyclerview.setLoadingMoreEnabled(false);
        mRecyclerview.setPullRefreshEnabled(true);
    }

    protected abstract boolean isHaveHead();

    /**
     * 当返回为true时候设置
     */
    protected abstract void setHeadViews();

    private void setHeadView(boolean isHaveHead) {
        if (!isHaveHead) {
            mHeadView.setVisibility(View.GONE);
        } else {
            mHeadView.setVisibility(View.VISIBLE);
        }
    }

    protected abstract MyBaseAdapter getAdapter();

    protected abstract int getLineNum();

}
